﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using throneOfGames.Models;
using System.Web.Security;
using System.Security;
using System.Security.Cryptography;

namespace throneOfGames.Controllers
{
    [HandleError]
    public class UserController : Controller
    {
        private IThroneRepository repo = null;

        public UserController()
        {
            repo = new ThroneRepository();
        }
        public UserController(IThroneRepository rep)
        {
            repo = rep;
        }

        public ActionResult Index()
        { 
            return View();
        }

        [Authorize]
        public ActionResult Admin() 
        {
            var users = from u in repo.AllUsers
                        select u;
            return View(users);
        }

        [HttpPost]
        public ActionResult Register(string userName, string password, string email)
        {
            try
            {
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                var pass = new System.Data.Linq.Binary(encoding.GetBytes(password));

                repo.insertUser(userName, pass, email);
            }
            catch
            {
                return View("Error");
            }

            return View();

        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string userName, string password)
        {
            try
            {
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                var pass = new System.Data.Linq.Binary(encoding.GetBytes(password));

                var user = (from u in repo.AllUsers
                            where u.userName == userName
                            && u.passw == pass
                            select u).SingleOrDefault();

                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(user.userName, true);
                    return View();
                }
                else
                {
                    ModelState.AddModelError("", "Login incorrect");
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }

        public ActionResult Logout()
        {
            try
            {
                FormsAuthentication.SignOut();
                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View("Error");
            }
        }

        [Authorize]
        public ActionResult ViewProfile(string id)
        {
            try
            {
                ProfileViewModel pVM = new ProfileViewModel();
                pVM.theUser = (from u in repo.AllUsers
                            where u.userName == id
                            select u).SingleOrDefault();

                pVM.leaderboardData = (from lbData in repo.AllLeaderboardData
                                       orderby lbData.score descending
                                       select lbData).ToList();

                if (pVM.theUser != null)
                {
                    return View(pVM);
                }
            }
            catch
            {
                return View("Error");
            }

            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult EditProfile(string ID, string userName, string passw, string email)
        {
            try
            {
                int userID = 0;

                int.TryParse(ID, out userID);

                var user = (from u in repo.AllUsers
                            where u.ID == userID
                            select u).SingleOrDefault();

                if (user != null)
                {
                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                    var pass = new System.Data.Linq.Binary(encoding.GetBytes(passw));

                    repo.updateUser(user.ID, userName, pass, email);

                    var obj = new { id = userName };
                    return Json(obj, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }
    }
}
